//
//  HLTCustomerserviceTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/9/6.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTCustomerserviceTableViewController.h"
#import "HLTCustomerserviceTableViewCell.h"
#import "HLTpickerview.h"
@interface HLTCustomerserviceTableViewController ()<UITextFieldDelegate>

@property UIView *mypickerview;


//view
@property (strong, nonatomic) IBOutlet UIButton *locationBtn;
@property (strong, nonatomic) IBOutlet UIPickerView *myPicker;
@property (strong, nonatomic)  HLTpickerview *pickerBgView;
@property (strong, nonatomic) UIView *maskView;





@property (strong, nonatomic) IBOutlet UIButton *provinceBtn;
@property (strong, nonatomic) IBOutlet UIButton *cityBtn;
@property (strong, nonatomic) IBOutlet UIButton *townBtn;
//data
@property (strong, nonatomic) NSDictionary *pickerDic;
@property (strong, nonatomic) NSArray *provinceArray;
@property (strong, nonatomic) NSArray *cityArray;
@property (strong, nonatomic) NSArray *townArray;
@property (strong, nonatomic) NSArray *selectedArray;





@end

@implementation HLTCustomerserviceTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.navigationItem setTitle:@"在线客服"];
    
//    self.type=[[NSString alloc]init];
    _pickerBgView=[[NSBundle mainBundle] loadNibNamed:@"pickerview" owner:self options:nil][0];
    
    
    self.maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 ,screen_width,screen_height)];
    self.maskView.backgroundColor = [UIColor blackColor];
    self.maskView.alpha = 0;
    [self.maskView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMyPicker)]];
    
    
    
    
//    _pickerBgView=[[HLTpickerview alloc]init];
     self.pickerBgView.width = screen_width;
    _myPicker=_pickerBgView.mypickerview;
    
//    NSLog(@"%f,%f",_pickview.frame.size.width,_pickview.frame.size.height);
//    _pickerBgView.frame=CGRectMake(0, 0, _pickview.frame.size.width, _pickview.frame.size.height);
    
//    [_pickview addSubview:_pickerBgView];
    
    _myPicker.delegate=self;
    
    
    //添加完成按钮事件
    [_pickerBgView.wancheng addTarget:self action:@selector(wancheng:) forControlEvents:UIControlEventTouchUpInside];
    //添加取消按钮事件
    [_pickerBgView.quxiao addTarget:self action:@selector(quxiao:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //    指定Delegate
//    _pickview.mypickerview.delegate=self;
    //    显示选中框
    _myPicker.showsSelectionIndicator=YES;
    NSArray *dataArray = [[NSArray alloc]initWithObjects:@"留言",@"投诉",@"询问",@"售后",@"求购",nil];
    
    _pickerData=dataArray;
    
    
//    _pickview.hidden=YES;
    
    
    self.info.delegate=self;

    self.zhuti.delegate=self;
    
    
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
    
    
     //去掉多余分割线的方法
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self.tableView setTableFooterView:view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



-(void) qiehuantype
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    [self.view addSubview:self.maskView];
    [self.view addSubview:self.pickerBgView];
    self.maskView.alpha = 0;
    self.pickerBgView.top = self.view.height;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.maskView.alpha = 0.3;
        self.pickerBgView.bottom = self.view.height;
    }];
    
    
}





-(void) wancheng:(id) sender
{
    NSInteger row =[self.myPicker selectedRowInComponent:0];
    
    self.type=[NSString stringWithFormat:@"%ld",(long)row];
    
    NSString *selected = [_pickerData objectAtIndex:row];
    self.liuyantype.text=selected;
    [self hideMyPicker];
    
    
}


-(void) quxiao:(id) sender
{
    [self hideMyPicker];
    
    
}


- (IBAction)tijiao:(id)sender {
     [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    if (self.type==nil) {
        
        self.type=@"0";
    }
    
    
    
    UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    
    
    //这个是一键解绑的事件
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    //发送在线客服(&username=18285117086&title=ceshi&type=1&content=cheshi  type 0 留言 1 投诉 2 询问 3 售后 4 求购)
    NSString *url=[zaixiankefu stringByAppendingFormat:@"&username=%@&title=%@&type=%@&content=%@",acc.custPhone,self.zhuti.text,self.type,self.info.text];
    
    [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
        
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"留言成功"
                                                      message:nil
                                                     delegate:nil
                                            cancelButtonTitle:@"好"
                                            otherButtonTitles:nil,nil];
        [alert show];

        
        
        
        
        
    } faliure:^(NSString *error) {
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
    }];
    
    
    
    
    
    
    
    
}



//隐藏pickerview的方法
- (void)hideMyPicker {
    
    
    [UIView animateWithDuration:0.3 animations:^{
        self.maskView.alpha = 0;
        self.pickerBgView.top = self.view.height;
    } completion:^(BOOL finished) {
        [self.maskView removeFromSuperview];
        [self.pickerBgView removeFromSuperview];
    }];
    
   
}



- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            
            [self qiehuantype];
            
            NSLog(@"点击了切换按钮");
            
        }
        else
        {
            [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
            
        }
        
    }
    else
    {
        
//        NSString *num = [[NSString alloc] initWithFormat:@"tel://96696021"]; //number为号码字符串 如果使用这个方法 结束电话之后会进入联系人列表
        
        
        
            NSString *num = [[NSString alloc] initWithFormat:@"telprompt://96696021"]; //而这个方法则打电话前先弹框  是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号

        
        
        
    }
}






- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        if (indexPath.row==2) {
            
            return (screen_width-30)*28/47.0+127+5;
        }
        else
            return 50;
    }
    else
        return 50;
    
    
    
    
}


























#pragma mark Picker Date Source Methods
//返回显示的列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//返回当前列显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_pickerData count];
}











#pragma mark Picker Delegate Methods

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_pickerData objectAtIndex:row];
}




#pragma mark texview的委托方法

//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
//{
//    NSIndexPath *localIndexPath = [NSIndexPath indexPathForRow:1 inSection:1];
//    
//    [self.tableView scrollToRowAtIndexPath:localIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//    
//    
//    
//    
//    return YES;
//}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
    //开始编辑时触发，文本字段将成为first responder
    
    //隐藏pickerview
    [self hideMyPicker];
//    NSIndexPath *localIndexPath = [NSIndexPath indexPathForRow:1 inSection:1];
//    
//    [self.tableView scrollToRowAtIndexPath:localIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    
}




- (void)viewDidAppear:(BOOL)animated
{
    
    NSLog(@"%f,%f",_pickview.frame.size.width,_pickview.frame.size.height);
}



- (void)viewDidLayoutSubviews
{
     NSLog(@"%f,%f",_pickview.width,_pickview.height);
    
}



//这个是回调方法
#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}









@end
